﻿using System;
using System.Collections.Generic;
using System.IO;

namespace changeFileAttributes
{
    static class FileHelper
    {
        // Collects Directories
        static public List<string> ListDirectories = new List<string>();

        public static List<string> GetFilesRecursive(string b)
        {
            // 1.
            // Store results in the file results list.
            List<string> result = new List<string>();

            // 2.
            // Store a stack of our directories.
            Stack<string> stack = new Stack<string>();

            // 3.
            // Add initial directory.
            stack.Push(b);

            // 4.
            // Continue while there are directories to process
            while (stack.Count > 0)
            {
                // A.
                // Get top directory
                string dir = stack.Pop();

                try
                {
                    // B
                    // Add all files at this directory to the result List.
                    result.AddRange(Directory.GetFiles(dir, "*.*"));

                    // C
                    // Add all directories at this directory.
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                        ListDirectories.Add(dn);
                    }
                }
                catch
                {
                    // D
                    // Could not open the directory
                }
            }
            return result;
        }


        static public void ChangeAttributes(string sFilePath, FileAttribute[] attribute)
        {
            if (sFilePath.Length > 0)
            {
                for (int i = 0; i < attribute.Length; i++)
                {
                    if (File.GetAttributes(sFilePath) == FileAttributes.Directory)
                    {
                        DirectoryInfo dirInfo = new DirectoryInfo(sFilePath);
                        if (attribute[i].TypeToString() == "Hidden")
                        {
                            dirInfo.Attributes = (attribute[i].bType
                                                      ? FileAttributes.Hidden
                                                      : ~FileAttributes.Hidden);
                        }
                        else if (attribute[i].TypeToString() == "Archive")
                        {
                            dirInfo.Attributes = (attribute[i].bType
                                                      ? FileAttributes.Archive
                                                      : ~FileAttributes.Archive);
                        }
                    }
                    else
                    {
                        if (attribute[i].TypeToString() == "Hidden")
                        {
                            // set or clear hidden attribute
                            File.SetAttributes(sFilePath,
                                               File.GetAttributes(sFilePath) & (attribute[i].bType
                                                                                    ? FileAttributes.Hidden
                                                                                    : ~FileAttributes.Hidden));
                        }
                        else if (attribute[i].TypeToString() == "Archive")
                        {
                            // set or clear archive 
                            File.SetAttributes(sFilePath,
                                               File.GetAttributes(sFilePath) & (attribute[i].bType
                                                                                    ? FileAttributes.Archive
                                                                                    : ~FileAttributes.Archive));
                        }
                    }
                }
            }
        }
    }


    public abstract class FileAttribute
    {
        public bool bType;
        public abstract string TypeToString();
    }

    public class AttHidden : FileAttribute
    {
        public AttHidden(bool _bType)
        {
            bType = _bType;
        }
        public override string TypeToString()
        {
            return "Hidden";
        }
    }

    public class AttArchive : FileAttribute
    {
        public AttArchive(bool _bType)
        {
            bType = _bType;
        }
        public override string TypeToString()
        {
            return "Archive";
        }
    }

    public class AttReadOnly : FileAttribute
    {
        public AttReadOnly(bool _bType)
        {
            bType = _bType;
        }
        public override string TypeToString()
        {
            return "ReadOnly";
        }
    }

    public class AttSystem : FileAttribute
    {
        public AttSystem(bool _bType)
        {
            bType = _bType;
        }
        public override string TypeToString()
        {
            return "System";
        }
    }
}
